package cn.smbms.controller;

import cn.smbms.pojo.Bill;
import cn.smbms.pojo.Provider;
import cn.smbms.pojo.User;
import cn.smbms.service.bill.BillService;
import cn.smbms.service.provider.ProviderService;
import cn.smbms.tools.Constants;
import com.alibaba.fastjson.JSONArray;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @program: smbms02
 * @description:
 * @author: wlg
 * @create: 2021-07-27 08:57:42
 **/
@Controller
@RequestMapping("/bill")
public class BillController {
    @Autowired
    private ProviderService providerService;
    @Autowired
    private BillService billService;

    @RequestMapping("/list")
    public String billList(
            @RequestParam(value = "queryProductName", defaultValue = "") String queryProductName,
            @RequestParam(value = "queryProviderId", defaultValue = "0") Integer queryProviderId,
            @RequestParam(value = "queryIsPayment", defaultValue = "0") Integer queryIsPayment,
            Model model
    ) {
        List<Provider> providerList = new ArrayList<Provider>();
        providerList = providerService.getProviderList("", "");
        model.addAttribute("providerList", providerList);

        List<Bill> billList = new ArrayList<Bill>();
        Bill bill = new Bill();
        bill.setIsPayment(queryIsPayment);
        bill.setProviderId(queryProviderId);
        bill.setProductName(queryProductName);
        billList = billService.getBillList(bill);
        model.addAttribute("billList", billList);
        model.addAttribute("queryProductName", queryProductName);
        model.addAttribute("queryProviderId", queryProviderId);
        model.addAttribute("queryIsPayment", queryIsPayment);
        return "billlist";
    }

    @GetMapping("/view/{billid}")
    public String viewBill(@PathVariable("billid") String id, Model model) {
        if (!StringUtils.isNullOrEmpty(id)) {
            Bill bill = null;
            bill = billService.getBillById(id);
            model.addAttribute("bill", bill);
            return "billview";
        } else {
            return "error";
        }
    }

    @GetMapping("/tomodify")
    public String toModify(@RequestParam("billid") String id, Model model) {
        if (!StringUtils.isNullOrEmpty(id)) {
            Bill bill = null;
            bill = billService.getBillById(id);
            model.addAttribute("bill", bill);
            return "billmodify";
        } else {
            return "error";
        }
    }

    @PostMapping("/domodify")
    public String doModify(Bill bill, HttpSession session) {
        bill.setModifyBy(((User) session.getAttribute(Constants.USER_SESSION)).getId());
        bill.setModifyDate(new Date());
        boolean flag = false;
        flag = billService.modify(bill);
        if (flag) {
            return "redirect:/bill/list";
        } else {
            return "billmodify";
        }
    }

    @GetMapping("/delete")
    @ResponseBody
    public String delete(@RequestParam("billid") String id) {
        HashMap<String, String> resultMap = new HashMap<String, String>();
        if (!StringUtils.isNullOrEmpty(id)) {
            boolean flag = billService.deleteBillById(id);
            if (flag) {//删除成功
                resultMap.put("delResult", "true");
            } else {//删除失败
                resultMap.put("delResult", "false");
            }
        } else {
            resultMap.put("delResult", "notexit");
        }
        return JSONArray.toJSONString(resultMap);
    }

    @GetMapping("/toadd")
    public String toAdd() {
        return "billadd";
    }

    @PostMapping("/doadd")
    public String doAdd(HttpSession session, Bill bill) {
        bill.setCreatedBy(((User) session.getAttribute(Constants.USER_SESSION)).getId());
        bill.setCreationDate(new Date());
        boolean flag = false;
        flag = billService.add(bill);
        System.out.println("add flag -- > " + flag);
        if (flag) {
            return "redirect:/bill/list";
        } else {
            return "billadd";
        }
    }
}
